﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using HiStaff.Dal;
using System.IO;
using HiStaff.Util;
using HiStaff.Control;

namespace HiStaff.UI
{
    public partial class CreateDBa : Form
    {
        public CreateDBa()
        {
            this.Font = HiSystem.FontSegoeUi;
            InitializeComponent();
        }

        private void btnExit_Click(object sender, EventArgs e)
        {
            this.Hide();
        }

        private void btnTest_Click(object sender, EventArgs e)
        {
            try
            {
                if (DBConnection.TestConnection(txtUsername.Text, txtPassword.Text, txtServer.Text, txtPort.Text, txtService.Text))
                {
                    MessageBox.Show("Connect successful", this.Text, MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
                else
                {
                    MessageBox.Show("Connect fail", this.Text, MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, this.Text, MessageBoxButtons.OK, MessageBoxIcon.Error);
                Log.Instance.WriteExceptionLog(ex, this.Name + "[" + this.Text + "]");
            }
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            string filename = "";
            string result = null;
            StreamWriter streamWriter;
            var sfDlg = new SaveFileDialog();
            string tmp = null;
            try
            {
                sfDlg.InitialDirectory = Path.GetDirectoryName(Application.ExecutablePath);
                sfDlg.Filter = "*.dba|*.dba";
                sfDlg.ShowDialog();
                filename = sfDlg.FileName;
                if (!string.IsNullOrEmpty(filename))
                {
                    result = string.Format(Constant.DEFAULT_FORMAT_CONNECTION,
                                           txtUsername.Text, txtPassword.Text,
                                           txtServer.Text, txtPort.Text,
                                           txtService.Text);

                    result = txtUsername.Text + (char)13 +
                             txtPassword.Text + (char)13 +
                             txtServer.Text + (char)13 +
                             txtPort.Text + (char)13 +
                             txtService.Text + (char)13 +
                             result;

                    tmp = CryptographyManager.Encrypt(result);
                    streamWriter = File.CreateText(filename);
                    streamWriter.Write("");
                    streamWriter.Write(tmp);
                    streamWriter.Close();
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("Can't create connecting file.", this.Text, MessageBoxButtons.OK, MessageBoxIcon.Error);
                Log.Instance.WriteExceptionLog(ex, this.Name + "[" + this.Text + "]");
            }
        }

        private void btnLoad_Click(object sender, EventArgs e)
        {
            string filename = "";
            string tmp = null;
            string inputString = null;
            StreamReader streamReader;
            var ofDlg = new OpenFileDialog();
            string[] TempArr = null;
            try
            {
                ofDlg.InitialDirectory = Path.GetDirectoryName(Application.ExecutablePath);
                ofDlg.Filter = "*.dba|*.dba";
                ofDlg.ShowDialog();
                filename = ofDlg.FileName;
                if (!string.IsNullOrEmpty(filename))
                {
                    streamReader = File.OpenText(filename);
                    inputString = streamReader.ReadLine();
                    streamReader.Close();
                    tmp = CryptographyManager.Decrypt(inputString);

                    TempArr = tmp.Split((char)13);

                    txtUsername.Text = TempArr[0];
                    txtPassword.Text = TempArr[1];
                    txtServer.Text = TempArr[2];
                    txtPort.Text = TempArr[3];
                    txtService.Text = TempArr[4];
                }
                
            }
            catch (Exception ex)
            {
                Log.Instance.WriteExceptionLog(ex, this.Name + "[" + this.Text + "]");
            }
        }
    }
}
